package com.ww.juc.jd.async.basic;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.jd.platform.async.callback.ICallback;
import com.jd.platform.async.callback.IWorker;
import com.jd.platform.async.worker.WorkResult;
import com.jd.platform.async.wrapper.WorkerWrapper;

import cn.hutool.core.date.SystemClock;
import cn.hutool.http.HttpUtil;

/**
 * 星座worker
 * @author wanggw
 * 2024年2月19日 下午5:51:57
 */
public class ConstellationWorker implements IWorker<String, String>, ICallback<String, String> {

	public static final String APPKEY = "xxxxxx";// 你的appkey

	public static final String URL = "https://www.baidu.com";

	@Override
	public void begin() {
		System.out.println(Thread.currentThread().getName() + "--开始--" + System.currentTimeMillis());
	}

	@Override
	public void result(boolean success, String param, WorkResult<String> workResult) {
		if (success) {
			System.out.println("回调星座成功--" + SystemClock.now() + "--" + workResult.getResult() + "-threadName:" + Thread.currentThread().getName());
		} else {
			System.out.println("回调星座失败--" + SystemClock.now() + "--" + workResult.getResult() + "-threadName:" + Thread.currentThread().getName());
		}
	}

	@SuppressWarnings({ "rawtypes", "unused" })
	@Override
	public String action(String object, Map<String, WorkerWrapper> allWrappers) {
		String url = URL;
	      String result = HttpUtil.get(url);

	      // 模拟异常
	      int i = 1/0;

	      // 模拟时长
	      try {
	         TimeUnit.SECONDS.sleep(5);
	      } catch (Exception e) {
	         System.err.println("[星座]>>>> 异常:" + e.getMessage());
	      }

	      return result;
	}

	@Override
	public String defaultValue() {
		return "默认星座worker";
	}

}
